// 登录表单校验规则
export const loginRules = {
  username: [
    { required: true, message: '账号不能为空', trigger: 'blur' },
    {
      min: 2,
      max: 20,
      message: '账号长度应为2-20个字符',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      min: 6,
      message: '密码长度至少为6个字符',
      trigger: 'blur'
    }
  ]
}

// 注册表单校验规则
export const registerRules = {
  username: [
    { required: true, message: '账号不能为空', trigger: 'blur' },
    {
      min: 2,
      max: 20,
      message: '账号长度应为2-20个字符',
      trigger: 'blur'
    }
  ],
  name: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    {
      min: 2,
      max: 20,
      message: '用户名长度应为2-20个字符',
      trigger: 'blur'
    }
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    {
      min: 6,
      message: '密码长度至少为6个字符',
      trigger: 'blur'
    }
  ],
  confirmPassword: [
    { required: true, message: '确认密码不能为空', trigger: 'blur' },
    {
      // 在组件内部使用时，通过第四个参数source获取表单数据
      validator: (rule, value, callback, source) => {
        // 安全地检查source和password属性是否存在
        if (
          value &&
          source &&
          source.password !== undefined &&
          value !== source.password
        ) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}
